package com.easytouch.vmhdl.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.easytouch.vmhdl.R;
import com.easytouch.vmhdl.activity.ManagerActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

/**
 * Created by gulin on 2018/1/11.
 */

public class BaseMgrFragment extends Fragment {

    protected DisplayImageOptions options;
    protected SimpleImageLoadingListener mLoadingListener;
    protected ImageLoader mImageLoader = ImageLoader.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new DisplayImageOptions.Builder()
                .cacheInMemory()
                .cacheOnDisc()
                .showImageOnFail(R.mipmap.ic_launcher)
                .build();
        mLoadingListener = new SimpleImageLoadingListener();
    }

    protected void goToFragment(final int toFragment, final Bundle bundle)
    {
        ManagerActivity activity = (ManagerActivity)this.getActivity();
        if(activity != null)
        {
            activity.goFragment(toFragment, bundle);
        }
    }

    protected void setTitle(final int pResId)
    {
        ManagerActivity activity = (ManagerActivity)this.getActivity();
        if(activity != null)
        {
            activity.setTitle(pResId);
        }
    }

    protected class SimpleImageLoadingListener implements ImageLoadingListener {

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {}

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {}

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason failReason) {
            if(failReason.getType() == FailReason.FailType.OUT_OF_MEMORY){
                mImageLoader.clearMemoryCache();
            }
        }

        @Override
        public void onLoadingStarted(String arg0, View arg1) {}
    }
}
